var arr = [];//存所有数据
var count = 8;//一页多少条数据
var page = 1;//当前的页数
var n;
var user = JSON.parse(localStorage.getItem('user'));
var arr2 = [];
arr2.push(user);
console.log(user.type);
console.log(user.name);


if (user.length) {
    function rem() {
        $('.motaikuang table tbody').html('');
        if (user.type == 2) {
            $.each(arr.slice((page - 1) * count, page * count), function (i, v) {
                var a = v.type == '1' ? '普通管理员' : "超级管理员";
                $('.motaikuang table tbody').append('<tr data-pass="' + v.password + '">\
    <td class="th1">'+ v.id + '</td>\
    <td>'+ v.name + '</td>\
    <td>'+ v.userId + '</td>\
    <td>'+ a + '</td>\
    <td>\
            <button type="button" class="shu" data-toggle="modal" data-target="#myModal5">\
                删除\
            </button>\
            <button type="button" class="shu"\ data-toggle="modal"data-target="#exampleModal2"\
                data-whatever="@mdo">修改</button>\
    </td>\
</tr>'
                )
            })
        }
    }
    function rwt() {
        $.ajax({
            url: 'http://172.17.7.212:3000/admin/getadmin',
            success: function (res) {
                console.log(res)
                if (res.data.length) {
                    arr = res.data;
                    rem();
                    ret();
                }

            },
        })
    }
    rwt();
    // 页码的点击
    $('#nav2').on('click', 'li', function () {
        page = $(this).text();
        rem();
    })
    function ret() {
        if (user.type == 2) {
            var n = Math.ceil(arr.length / count);
            $('#nav2 .diyebiao').remove();
            for (var i = 1; i <= n; i++) {
                $('.yeshu').before($('<li class="diyebiao"><a href="#">' + i + '</a></li>'));
            }
        } else if (user.type == 1) {
            var n = Math.ceil(arr2.length / count);
            $('#nav2 .diyebiao').remove();
            for (var i = 1; i <= n; i++) {
                $('.yeshu').before($('<li class="diyebiao"><a href="#">' + i + '</a></li>'));
            }
        }
    }

    //向前
    $('.j-previous').on('click', function () {

        page--;
        //   console.log(page)
        if (page <= 1) {

            page = 1;
        }

        rwt();
    })
    //向后
    $('.yeshu').on('click', function () {

        page++;
        //   console.log(page)
        if (page >= Math.ceil(arr.length / count)) {
            page = Math.ceil(arr.length / count);
        }
        rwt();
    })
    // 添加
    $('.j-add-btn').on('click', function () {
        if (user.type == 2) {
            // console.log(11);
            $.ajax({
                url: 'http://172.17.7.212:3000/admin/addadmin',
                data: {
                    name: $('.j-add-name').val(),
                    password: $('.j-add-password').val(),
                    type: $('.j-add-type').val(),
                    userId: $('.j-add-user').val(),
                },

                type: 'post',
                success: function (res) {
                    // console.log(res)
                    if (res.code == 200) {
                        $('#exampleModal1').modal('hide');
                        rwt();
                    } else {
                        alert('添加失败')
                    }
                }
            })
        } else if (user.type == 1) {
            window.location.href = '/wb258-2/page/HomePage.html'
        }
    })
    //删除
    // 当点击确定删除按钮的时候
    if (user.type == 2) {
        $('.guanlishanchu').on('click', function () {
            $.ajax({
                url: '/api/admin/deladmin',
                data: {
                    id: n
                },
                type: 'post',
                success: function (res) {
                    // console.log(res)
                    if (res.code == 200) {
                        $('#myModal5').modal('hide');
                        rwt();
                    } else {
                        alert('删除失败')
                    }
                }
            })
        })
    } else if (user.type == 1) {
        window.location.type = '/wb258-2/page/HomePage.html'
    }

    // 点击删除的时候
    if (user.type == 2) {
        $('#myModal5').on('show.bs.modal', function (e) {
            n = $(e.relatedTarget).parents('tr').children().eq(0).text();

        })
    } else if (user.type == 1) {
        window.location.type = '/wb258-2/page/HomePage.html'
    }
    //修改
    // 当点击修改的时候

    $('#exampleModal2').on('show.bs.modal', function (e) {
        var tr = $(e.relatedTarget).parents('tr');
        $('.j-s-id').val(tr.children().eq(0).text());
        $('.j-s-name').val(tr.children().eq(1).text());

        $('.j-s-wp').val(tr.children().eq(2).text());

        $('.j-s-pass').val(tr.attr('data-pass'));

        $('.j-s-type option[value=' + tr.children().eq(3).text() + ']').attr('selected', 'selected');


    })

    // 当点击确定的时候

    $('.j-s-q').on('click', function () {

        $.ajax({
            url: '/api/admin/updateadmin',
            data: {
                id: $('.j-s-id').val(),
                name: $('.j-s-name').val(),
                userId: $('.j-s-wp').val(),
                password: $('.j-s-pass').val(),
                type: $('.j-s-type').val() == "普通管理员" ? 1 : 2
            },
            type: 'post',
            success: function (res) {
                if (res.code == 200) {
                    $('#exampleModal2').modal('hide');
                    rwt();
                } else {
                    alert("修改失败")
                }
            }
        })
    })
    //切换账号
    $('#qiehuanzhanghao').on('click', function () {
        window.location.href = '/wb258-2/page/HomePage.html'
    })
    // 退出
    $('#guanbichuangko').on('click', function () {
        closePage();
        // window.location.href="https://blog.csdn.net/kuangfengbuyi/article/details/52052301"

    })
    function closePage() {
        if (navigator.userAgent.indexOf("Firefox") != -1 || navigator.userAgent.indexOf("Chrome") != -1) {
            window.location.href = "about:blank";
            window.close();
        } else {
            window.opener = null;
            window.open("", "_self");
            window.close();
        }
    }
} else {
    window.location.href = '/wb258-2/page/HomePage.html'
}